package com.game.module.skill
{
    import com.dusk.util.ResUtil;
    import com.dusk.util.StringUtil;
    import com.game.data.vo.PlayerDataVO;
    import com.game.data.vo.SkillVO;
    import com.game.manager.NotificationManager;
    import com.game.view.layer.LayerBase;
    
    import flash.display.MovieClip;
    import flash.display.SimpleButton;
    import flash.events.*;
    
    public class PassiveSkill extends LayerBase
	{
		
		public function PassiveSkill(data:PlayerDataVO)
		{
			player = data;
			super();
		}
		
		public var skill1:SinglePassiveSkill;
		public var skill2:SinglePassiveSkill;
		public var skill3:SinglePassiveSkill;
		public var skill4:SinglePassiveSkill;
		public var skill5:SinglePassiveSkill;
		
		public var btn1:SimpleButton;
		public var btn2:SimpleButton;
		public var btn3:SimpleButton;
		public var btn4:SimpleButton;
		public var btn5:SimpleButton;
		
		private var player:PlayerDataVO;
		
		override protected function beforeAdded():void
		{
			var mc:MovieClip = ResUtil.getLayerView("PassiveSkill") as MovieClip;
			for (var i:int = 1; i <= 5; i++)
			{
				this["skill" + i] = new SinglePassiveSkill();
				(this["skill" + i] as SinglePassiveSkill).setSkillVo(gc.configManager.skillProxy.getPassiveSkillByIndex(i).clone());
				(this["skill" + i] as SinglePassiveSkill).setPlayer(player);
				this["skill" + i].x = 124;
				this["skill" + i].y = i * 79 + 66;
				mc.addChild(this["skill" + i]);
			}
			btn1 = mc.getChildByName("btn1") as SimpleButton;
			btn2 = mc.getChildByName("btn2") as SimpleButton;
			btn3 = mc.getChildByName("btn3") as SimpleButton;
			btn4 = mc.getChildByName("btn4") as SimpleButton;
			btn5 = mc.getChildByName("btn5") as SimpleButton;
			addChild(mc);
		}
		
		override protected function onAdded():void
		{
			for (var i:int = 1; i <= 5; i++)
			{
				(this["btn" + i] as SimpleButton).addEventListener(MouseEvent.CLICK, upgradeSkill);
			}
			refreshPanel();
		}
		
		override protected function onRemoved():void
		{
			for (var i:int = 1; i <= 5; i++)
			{
				(this["btn" + i] as SimpleButton).removeEventListener(MouseEvent.CLICK, upgradeSkill);
			}
			player = null;
		}
		
		private function refreshPanel():void
		{
			for (var i:int = 1; i <= 5; i++)
			{
				(this["skill" + i] as SinglePassiveSkill).refreshPanel();
			}
		}
		
		private function upgradeSkill(evt:MouseEvent):void
		{
			var skillIndex:int = parseInt(StringUtil.getLastMultiChars(evt.currentTarget.name,1));
			var vo:SkillVO = gc.configManager.skillProxy.getPassiveSkillByIndex(skillIndex);
			if(player.getPassiveSkillLevelByIndex(skillIndex) >= vo.maxLevel)
			{
				NotificationManager.alert("技能等级已达上限");
				return;
			}
			if(player.lhValue < vo.getUpGradeCostByLevel(player.getPassiveSkillLevelByIndex(skillIndex) + 1))
			{
				NotificationManager.alert("灵魂不足");
				return;
			}
			player.passiveSkill[skillIndex - 1] ++;
			NotificationManager.alert("技能升级成功");
			(parent as ManageSkill).refreshPanel();
			refreshPanel();
			if(gc.isInBattle)
			{
				gc.team.hero.getAttribute().addPassive(skillIndex - 1,player.passiveSkill[skillIndex - 1],true);
			}
		}
	}
}
